package com.example.helloworld.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.example.helloworld.persistence.IconTextItem;
import com.example.helloworld.view.IconTextView;

public class IconTextListAdapter extends BaseAdapter {
	private Context context;
	private List<IconTextItem> items =	new ArrayList<IconTextItem>();
	
	public IconTextListAdapter(Context context) {
		this.context =	context;
	}
	
	public void addItem(IconTextItem item) {
		items.add(item);
	}
	
	public void setListItems(List<IconTextItem> list) {
		items =	list;
	}
	
	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}
	
	public boolean areAllItemsSelectable() {
		return false;
	}
	
	public boolean isSelectable(int postion) {
		try {
			return items.get(postion).isSelectable();
		} catch (IndexOutOfBoundsException e) {
			return false;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		IconTextView view;
		
		if (convertView == null) {
			view =	new IconTextView(context, items.get(position));
		} else {
			view =	(IconTextView)convertView;
			
			view.setIcon(items.get(position).getIcon());
			view.setText(0, items.get(position).getData(0));
			view.setText(1, items.get(position).getData(1));
			view.setText(2, items.get(position).getData(2));
		}
		return view;
	}
}
